package com.nys.cqhttp.service.impl;

import java.util.*;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.nys.cqhttp.mapper.FilterMassageMapper;
import com.nys.cqhttp.mapper.TheMessageMapper;
import com.nys.cqhttp.mapper.UserConfigMapper;
import com.nys.cqhttp.model.*;
import com.nys.cqhttp.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.nys.cqhttp.message.factory.MessageFactory;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.service.IMsgService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * GroupMsgServiceImpl
 *
 * @author nysheng
 * @version 1.0.0
 * @description 群组消息处理业务实现
 * @date 2021/12/27 14:28
 */
@Service("groupMsgServiceImpl")
@Slf4j
public class GroupMsgServiceImpl implements IMsgService {

  //  @Autowired private CqHttpRepository cqHttpRepository;

  @Autowired private MessageFactory messageFactory;
  @Autowired private FilterMassageMapper filterMassageMapper;
  @Autowired private UserConfigMapper userConfigMapper;
  @Autowired private TheMessageMapper theMessageMapper;

  @Override
  public void start(MessageParam messageParam) {
    String atSelf = "[CQ:at,qq=" + messageParam.getSelfId() + "]";
    if (!messageParam.getMessage().contains(".-")) {
      TheMessageExample theMessageExample = new TheMessageExample();
      TheMessageExample.Criteria criteria = theMessageExample.createCriteria();
      criteria.andJqrQqEqualTo(messageParam.getSelfId());
      criteria.andUserQqEqualTo(messageParam.getSenderId());
      criteria.andMessageLike("CF-%");
      theMessageMapper.deleteByExample(theMessageExample);
    }
    List<String> list = Arrays.asList("剪刀", "石头", "布");
    String[] split = messageParam.getMessage().split("-");
    if (!list.contains(split[0])) {
      TheMessageExample theMessageExample = new TheMessageExample();
      TheMessageExample.Criteria criteria = theMessageExample.createCriteria();
      criteria.andJqrQqEqualTo(messageParam.getSelfId());
      criteria.andUserQqEqualTo(messageParam.getSenderId());
      criteria.andMessageEqualTo("游戏1");
      theMessageMapper.deleteByExample(theMessageExample);
    }
    boolean res = false;
    // 过滤拦截
    FilterMassageExample example = new FilterMassageExample();
    FilterMassageExample.Criteria criteria = example.createCriteria();
    criteria.andUserEqualTo(messageParam.getSelfId());
    List<FilterMassage> filterMassages = filterMassageMapper.selectByExample(example);
    for (FilterMassage filterMassage : filterMassages) {
      if (filterMassage.getType() != 1) {
        if (messageParam.getMessage().contains(filterMassage.getValue())) {
          res = true;
          if (filterMassage.getType() == 3) {
            messageParam.setMessage(
                messageParam.getMessage().substring(filterMassage.getValue().length() + 2));
          }
        }
      } else {
        if (StringUtils.equals(filterMassage.getValue(), messageParam.getMessage())) {
          res = true;
        }
      }
    }

    if (!messageParam.getMessage().contains(atSelf) && !res) {
      return;
    }
    log.info(
        "收到群聊【{}】成员【{}】的消息：【{}】",
        messageParam.getGroupId(),
        messageParam.getSenderId(),
        messageParam.getMessage());
    UserConfigExample example1 = new UserConfigExample();
    UserConfigExample.Criteria criteria1 = example1.createCriteria();
    criteria1.andQqEqualTo(messageParam.getSelfId());
    List<UserConfig> userConfigs = userConfigMapper.selectByExample(example1);
    // 过滤消息内容
    messageParam.setMessage(messageParam.getMessage().replace(atSelf, "").trim());
    messageParam.setUrl(userConfigs.get(0).getUrl());
    List<String> messageList = messageFactory.getMessage(messageParam);
    messageList.forEach(
        message -> {
          if (StringUtils.isNotBlank(message)) {
            RestTemplate restTemplate = SpringContextUtil.getBean("restTemplate");

            restTemplate.getForEntity(userConfigs.get(0).getUrl()+"/send_msg?message_type="+messageParam.getMessageType()
                            +"&user_id="+messageParam.getSenderId()+"&message="+message+"&group_id="+messageParam.getGroupId(),String.class);
          }
        });
  }
}
